package com.jishuo.base.controller;

import com.jishuo.base.annotation.UserAuthority;
import com.jishuo.base.bean.BaseResult;
import com.jishuo.base.bean.ErrorCodeEnum;
import com.jishuo.base.bean.GridResult;
import com.jishuo.base.bean.Pagination;
import com.jishuo.base.bean.vo.UserVO;
import com.jishuo.base.service.UserService;
import com.jishuo.nontax.bean.vo.BankStationVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Slf4j
@Controller
@RequestMapping(value = "/sys/user", name = "系统用户")
@UserAuthority(menu = "sys/user/index")
public class UserController {

    @Resource
    private UserService userService;

    // 页面文件位置
    private String prefix = "sys/user/";

    @RequestMapping(value = {"index"}, name = "首页")
    public String index() {
        return prefix + "index";
    }

    @RequestMapping(value = {"userPassword", "userPassword/{page}"}, name = "修改密码")
    public String changePassword(@PathVariable(required = false) String page) {
        return prefix + ((page == null) ? "userPassword" : page);
    }

    @ResponseBody
    @RequestMapping(value = "listUser", name = "用户列表")
    public GridResult<UserVO> listUser(UserVO user, Pagination pagination) {
        return new GridResult<>(this.userService.listUser(user, pagination));
    }

    @ResponseBody
    @RequestMapping(value = "getUser", name = "获得用户")
    public BaseResult getUser(int userId) {
        UserVO user = this.userService.getUser(userId);
        user.setUserPwd("******");
        if (user == null)
            return new BaseResult(ErrorCodeEnum.DATA_NOT_FOUND);
        else
            return new BaseResult(user);
    }


    @ResponseBody
    @RequestMapping(value = "addUser", name = "新增用户")
    public BaseResult addUser(UserVO user) {
        return this.userService.addUser(user);
    }


    @ResponseBody
    @RequestMapping(value = "saveUser", name = "更新用户")
    public BaseResult saveUser(UserVO user) {
        return this.userService.saveUser(user);
    }

    @ResponseBody
    @RequestMapping(value = "editBankUserId", name = "更新用户银行")
    public BaseResult editBankUserId(UserVO user) {
        return this.userService.editBankUserId(user);
    }


    @ResponseBody
    @RequestMapping(value = "editMenuUser", name = "更新用户菜单权限")
    public BaseResult editMenuUser(UserVO user) {
        return this.userService.editMenuUser(user);
    }

    @ResponseBody
    @RequestMapping(value = "deleteUser", name = "删除用户")
    public BaseResult deleteUser(int userId) {
        return this.userService.deleteUser(userId);
    }

    @ResponseBody
    @RequestMapping(value = "resetUserPwd", name = "重置密码")
    public BaseResult resetUserPwd(int userId) {
        return this.userService.resetUserPwd(userId);
    }

    @ResponseBody
    @RequestMapping(value = "listBankStation", name = "网点列表")
    public GridResult<BankStationVO> listBankStation() {
        return new GridResult<>(this.userService.listBankStation());
    }

    @ResponseBody
    @RequestMapping(value = "editPassword", name = "修改密码")
    public BaseResult editPassword(HttpSession session, String oldPassword, String newPassword) {
        UserVO user = (UserVO) session.getAttribute("login_user");
        return this.userService.editUserPwd(user,oldPassword,newPassword);
    }

}
